/**
 * Input prompt example
 */

//'use strict';
var inquirer = require('inquirer'),
    cp = require('child_process'),
    fs = require('fs');

var questions = [
    {
        type: 'input',
        name: 'project_name',
        message: 'What\'s your project name'
    },
    {
        type: 'input',
        name: 'install_path',
        message: 'What\'s your project install_path',
        default: function () {
            return __dirname;
        }
    },
    {
        type: 'confirm',
        name: 'isStrict',
        message: 'open strict mode',
        default: function () {
            return true;
        }
    },
    {
        type: 'confirm',
        name: 'isInstall',
        message: 'install the project now',
        default: function () {
            return true;
        }
    }
];

inquirer.prompt(questions).then(function (answers) {
    fs.writeFile('./lib/init.json', JSON.stringify(answers, null, '  '), function (err) {
        if (err) throw err;
})
    ;
    install();

});
//自动安装任务
function install() {
    fs.readFile('./lib/init.json', function (err, data) {
        if (err) throw err;
        var jsonObj = JSON.parse(data),
            project_name = jsonObj['project_name'],
            install_path = jsonObj['install_path'],
            isStrict = jsonObj['isStrict'],
            isInstall = jsonObj['isInstall'];
        if (isInstall) {
            console.log("running…");
            cp.exec('npm uninstall gulp -d', function (err) {

                if (err)  throw err;
                console.log("done!");
            })
        }
        else {
            console.log("uninstall");
        }
    })
}
//添加指定内容到文件
function add() {
    var testStr={"data":"add"};
    fs.readFile('./lib/init.json', function (err, data) {
        if (err) throw err;
        var jsonObj = JSON.parse(data);
            jsonObj.push(testStr);
    })
}
//删除指定文件内容
function del() {
	  var testStr={"data":"add"};
    fs.readFile('./lib/init.json', function (err, data) {
        if (err) throw err;
        var jsonObj = JSON.parse(data),
            project_name = jsonObj['project_name'];
        console.log(jsonObj);


    })
}
//修改指定文件内容
function edit() {
	  var testStr={"data":"edit"};
    fs.readFile('./lib/init.json', function (err, data) {
        if (err) throw err;
        var jsonObj = JSON.parse(data);
            jsonObj.push(testStr);
    })
}







